package main

import (
	"context"
	"fmt"
	"ghw2/client/config"
	"ghw2/client/connect"
	"ghw2/service"
	"google.golang.org/grpc"
	"google.golang.org/grpc/connectivity"
	"google.golang.org/grpc/credentials/insecure"
	"math/rand"
)

func main() {
	if err := config.ClientConfigInit("client.yaml"); err != nil {
		fmt.Println(err)
		return
	}
	if err := connect.ConnectNameSrvInit(); err != nil {
		fmt.Println(err)
		return
	}
	defer connect.Close()

	for {
		fmt.Print("\n* 输入1-4测试接口：1、添加书籍；2、按照Id查找书籍；3、按照name模糊查找书籍；4、通过Id删除书籍\n请输入：")
		var name string
		var ID int32
		var option int
		fmt.Scan(&option)
		switch option {
		case 1:
			fmt.Print("请输入书籍的名字：")
			fmt.Scan(&name)
			fmt.Print("请输入书籍的ID：")
			fmt.Scan(&ID)
			var book service.Book
			book.Name = name
			book.ID = ID

			addressList, err := connect.GetAddressByService("Book:add")
			if err != nil {
				fmt.Println("与Namesrv的通信不可用！")
				return
			}
			if len(*addressList) == 0 {
				fmt.Println("无服务可用！")
				continue
			}

			var conn *grpc.ClientConn
			start := rand.Intn(len(*addressList))
			conn, err = grpc.Dial((*addressList)[0], grpc.WithTransportCredentials(insecure.NewCredentials()))

			var list []string
			if err == nil {
				list = append((*addressList)[start+1:], (*addressList)[0:start]...)
			}

			for _, v := range list {
				conn, err = grpc.Dial(v, grpc.WithTransportCredentials(insecure.NewCredentials()))
				if err == nil {
					break
				}
			}

			if conn == nil || conn.GetState() == connectivity.Idle {
				fmt.Println("服务不可达！")
				continue
			}

			client := service.NewBookServiceClient(conn)
			res, err := client.Add(context.Background(), &book)
			if err != nil {
				fmt.Println(err)
				conn.Close()
				continue
			}
			fmt.Printf("error:\t%+v\nres:\t%+v", err, res)
			conn.Close()
		case 2:
			fmt.Print("请输入书籍的ID：")
			fmt.Scan(&ID)
			var bookId service.BookID
			bookId.ID = ID

			addressList, err := connect.GetAddressByService("Book:queryByID")
			if err != nil {
				fmt.Println("与Namesrv的通信不可用！")
				return
			}
			if len(*addressList) == 0 {
				fmt.Println("无服务可用！")
				continue
			}

			var conn *grpc.ClientConn
			start := rand.Intn(len(*addressList))
			conn, err = grpc.Dial((*addressList)[0], grpc.WithTransportCredentials(insecure.NewCredentials()))

			if err == nil {
				list := append((*addressList)[start+1:], (*addressList)[0:start]...)
				addressList = &list
			}

			for _, v := range *addressList {
				conn, err = grpc.Dial(v, grpc.WithTransportCredentials(insecure.NewCredentials()))
				if err == nil {
					break
				}
			}

			if conn == nil || conn.GetState() == connectivity.Idle {
				fmt.Println("服务不可达！")
				continue
			}

			client := service.NewBookServiceClient(conn)
			res, err := client.QueryByID(context.Background(), &bookId)
			if err != nil {
				fmt.Println(err)
				conn.Close()
				continue
			}
			fmt.Printf("error:\t%+v\nres:\t%+v", err, res)
			conn.Close()

		case 3:
			fmt.Print("请输入书籍的名字：")
			fmt.Scan(&name)

			var bookName service.BookName
			bookName.Name = name

			addressList, err := connect.GetAddressByService("Book:queryByName")
			if err != nil {
				fmt.Println("与Namesrv的通信不可用！")
				return
			}
			if len(*addressList) == 0 {
				fmt.Println("无服务可用！")
				continue
			}

			var conn *grpc.ClientConn
			start := rand.Intn(len(*addressList))
			conn, err = grpc.Dial((*addressList)[0], grpc.WithTransportCredentials(insecure.NewCredentials()))

			if err == nil {
				list := append((*addressList)[start+1:], (*addressList)[0:start]...)
				addressList = &list
			}

			for _, v := range *addressList {
				conn, err = grpc.Dial(v, grpc.WithTransportCredentials(insecure.NewCredentials()))
				if err == nil {
					break
				}
			}

			if conn == nil || conn.GetState() == connectivity.Idle {
				fmt.Println("服务不可达！")
				continue
			}

			client := service.NewBookServiceClient(conn)
			res, err := client.QueryByName(context.Background(), &bookName)
			fmt.Printf("error:\t%+v\nres:\t%+v", err, res)
			if err != nil {
				fmt.Println(err)
				conn.Close()
				continue
			}
			conn.Close()
		case 4:
			fmt.Print("请输入书籍的ID：")
			fmt.Scan(&ID)
			var bookId service.BookID
			bookId.ID = ID

			addressList, err := connect.GetAddressByService("Book:delete")
			if err != nil {
				fmt.Println("与Namesrv的通信不可用！")
				return
			}
			if len(*addressList) == 0 {
				fmt.Println("无服务可用！")
				continue
			}

			var conn *grpc.ClientConn
			start := rand.Intn(len(*addressList))
			conn, err = grpc.Dial((*addressList)[0], grpc.WithTransportCredentials(insecure.NewCredentials()))

			if err == nil {
				list := append((*addressList)[start+1:], (*addressList)[0:start]...)
				addressList = &list
			}

			for _, v := range *addressList {
				conn, err = grpc.Dial(v, grpc.WithTransportCredentials(insecure.NewCredentials()))
				if err == nil {
					break
				}
			}

			if conn == nil || conn.GetState() == connectivity.Idle {
				fmt.Println("服务不可达！")
				continue
			}

			client := service.NewBookServiceClient(conn)
			res, err := client.Delete(context.Background(), &bookId)
			if err != nil {
				fmt.Println(err)
				conn.Close()
				continue
			}
			fmt.Printf("error:\t%+v\nres:\t%+v", err, res)
			conn.Close()
		}
	}
}
